<template>
    <el-form ref="formRef" style="max-width: 400px" :model="formData" :rules="rules" label-width="auto"
        class="demo-ruleForm" status-icon>
        <el-form-item label="类目名称" prop="name">
            <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="类目图片" prop="img">
            <el-input v-model="formData.img" />
            <Tuku v-model="formData.img"></Tuku>
        </el-form-item>
        <el-form-item label="类目排序" prop="seq">
            <el-input-number v-model="formData.seq" :min="1" :max="10" />
        </el-form-item>
        <el-form-item label="类目父目录" prop="parentId">
            <el-tree-select v-model="formData.parentId" :data="categorydata" check-strictly :render-after-expand="false" style="width: 240px" />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm(formRef)">
                提交
            </el-button>
            <el-button @click="resetForm(formRef)">重置</el-button>
        </el-form-item>
    </el-form>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { categoryApi } from '@/api';
import { ElMessage } from 'element-plus'
//路由跳转
import { useRouter } from 'vue-router';
const router = useRouter()
import Tuku from '@/components/Tuku.vue';

const formRef = ref()
const formData = reactive({
    name: '',
    img: '',
    seq: 0,
    parentId: 0
})

const rules = reactive({
    name: [
        { required: true, message: '请输入商品名称', trigger: 'blur' },
        { min: 1, max: 15, message: '名称输入区间在1~15个字符', trigger: 'blur' },
    ],
})
const categorydata = ref([
    {
        value: '0',
        label: '一级目录',
    }
])

onMounted(() => { 
    CallcategoryTree()
})
const CallcategoryTree = () => { 

    categoryApi.select.call().then((res: any) => { 
        res.forEach((item:any)=> {
            item.value = item.id
            item.label=item.name
        });
        // console.log(res);
        const categorydata2 = res.reduce((hezi: any, currentvalue: any) => { 
            currentvalue.children = res.filter((item: any) => { 
                //console.log("item=",item);
                
                return item.parentId == currentvalue.id
            })
            if (currentvalue.parentId === 0) { 
                hezi.push(currentvalue)
            }
            return hezi;
        },[])
        console.log(categorydata2);
        categorydata.value = categorydata2

    })
}



const formsubmit = () => { 
    categoryApi.insert.call({
        name: formData.name, img: formData.img, seq: formData.seq,
        parentId:formData.parentId
    }).then((res: any) => { 
        console.log(res);
        ElMessage.success("恭喜您提交成功！！")
        router.push({name:'category-list'})
    })
}

const submitForm = async (formEl: any) => {
    if (!formEl) return
    await formEl.validate((valid: any, fields: any) => {
        if (valid) {
            console.log('submit!')
            formsubmit()

        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: any) => {
    if (!formEl) return
    formEl.resetFields()
}

</script>
<style scoped></style>